package com.heifeng.cloudshelf.mvp.show.presenter;

import android.graphics.Bitmap;

import androidx.viewpager.widget.ViewPager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.heifeng.cloudshelf.R;
import com.heifeng.cloudshelf.base.BaseResponse;
import com.heifeng.cloudshelf.base.RxPresenter;
import com.heifeng.cloudshelf.dao.BlueprintInfoBean;
import com.heifeng.cloudshelf.event.ImageClickEvent;
import com.heifeng.cloudshelf.helper.MyObserver;
import com.heifeng.cloudshelf.helper.RxUtil;
import com.heifeng.cloudshelf.mvp.programme.adapter.ProgrammeImageRvAdapter;
import com.heifeng.cloudshelf.mvp.programme.adapter.ViewpagerAdapter;
import com.heifeng.cloudshelf.mvp.show.contract.ShowDetailActivityContract;
import com.heifeng.cloudshelf.network.NetClient;
import com.heifeng.cloudshelf.utils.DensityUtils;
import com.heifeng.cloudshelf.utils.LogUtils;
import com.heifeng.cloudshelf.utils.LoginErrorUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import io.reactivex.disposables.Disposable;

/**
 * Created by admin on 2019/9/19.
 */

public class ShowDetailActivityPresenter extends RxPresenter<ShowDetailActivityContract.View> implements ShowDetailActivityContract.Presenter {
    @Inject
    public ShowDetailActivityPresenter() {
    }

    private LinearLayoutManager layoutManager;

    private ProgrammeImageRvAdapter adapter;

    private List<String> imageList = new ArrayList<>();

    private int rvFirstVisibleItem = 0;

    private int rvLastVisibleItem = 0;

    @Override
    public void init() {
        if (null != mView) {
            initRecyclerView(mView.getRecyclerView());
            initViewPager(mView.getViewPager());

            if (imageList.size() > 0)
                Glide.with(mContext)
                        .asBitmap()
                        .load(imageList.get(0))
                        .into(new SimpleTarget<Bitmap>() {
                            @Override
                            public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
                                resetImageSize(resource, resource.getHeight(), resource.getWidth());
                            }
                        });


            mView.getTvAllNum().setText("/" + imageList.size() + "张");
        }

        //setLongClickable是必须的
        //mView.getIvMain().setLongClickable(true);
        //mView.getIvMain().setOnTouchListener(new MyGestureListener(mContext));
    }

    public void initViewPager(ViewPager viewPager) {
        ViewpagerAdapter adapter1 = new ViewpagerAdapter(mContext, imageList);
        viewPager.setAdapter(adapter1);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {

                Glide.with(mContext)
                        .asBitmap()
                        .load(imageList.get(position))
                        .into(new SimpleTarget<Bitmap>() {
                            @Override
                            public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
                                resetImageSize(resource, resource.getHeight(), resource.getWidth());
                            }
                        });
                if (null != mView) {
                    adapter.setSelectPos(position);
                    mView.getTvSelect().setText("" + (position + 1));
                    adapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void getBlueprintInfo(String id) {
        NetClient.getInstance().getNetApi().getBlueprintInfo(id)
                .compose(RxUtil.<BaseResponse<BlueprintInfoBean>>rxSchedulers())
                .subscribe(new MyObserver<BaseResponse<BlueprintInfoBean>>(mContext) {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(BaseResponse<BlueprintInfoBean> baseResponse) {
                        if (0 == baseResponse.getCode()) {
                            if (null != mView) {
                                BlueprintInfoBean getresult = baseResponse.getresult();
                                if (null != getresult.getImageList() && getresult.getImageList().size() > 0) {
                                    imageList.clear();
                                    imageList = getresult.getImageList();
                                    init();
                                    String str = "";
                                    if (!TextUtils.isEmpty(baseResponse.getresult().getAreas())) {
                                        str = baseResponse.getresult().getAreas() + "㎡  ";
                                    }
                                    if (!TextUtils.isEmpty(baseResponse.getresult().getHouseType())) {
                                        str += baseResponse.getresult().getHouseType() + "  ";
                                    }
                                    if (!TextUtils.isEmpty(baseResponse.getresult().getBudget())) {
                                        str += baseResponse.getresult().getBudget()+"  ";
                                    }
                                    if (!TextUtils.isEmpty(baseResponse.getresult().getAddress())) {
                                        str += baseResponse.getresult().getAddress();
                                    }

                                    if (!TextUtils.isEmpty(str)) {
                                        str = "房屋情况：" + str;
                                    }
                                    LogUtils.e("str:" + str);
                                    mView.setImageTilte(str, baseResponse.getresult().getDeliver());
                                    mView.setCodeImage(baseResponse.getresult().getMiniQrUrl());
                                } else {
                                    LoginErrorUtils.loginOut(mContext, baseResponse.getCode(), baseResponse.getmsg());
                                }
                            }
                        }
                    }
                });
    }

    @Override
    public List<String> getList() {
        return imageList;
    }

    @Override
    public int getFirstItemPos() {
        return rvFirstVisibleItem;
    }

    @Override
    public int getLastItemPos() {
        return rvLastVisibleItem;
    }

    public void initRecyclerView(RecyclerView recyclerView) {
//        imageList.add("http://pic16.nipic.com/20111006/6239936_092702973000_2.jpg");
//        imageList.add("http://pic25.nipic.com/20121112/9252150_150552938000_2.jpg");
//        imageList.add("http://pic26.nipic.com/20121221/9252150_142515375000_2.jpg");
//        imageList.add("http://pic13.nipic.com/20110409/7119492_114440620000_2.jpg");
//        imageList.add("http://pic13.nipic.com/20110409/7119492_114440620000_2.jpg");
//        imageList.add("http://pic68.nipic.com/file/20150601/8164280_104301508000_2.jpg");
//        imageList.add("http://pic68.nipic.com/file/20150601/8164280_104301508000_2.jpg");
//        imageList.add("http://pic16.nipic.com/20111006/6239936_092702973000_2.jpg");
//        imageList.add("http://pic16.nipic.com/20111006/6239936_092702973000_2.jpg");
//        imageList.add("http://pic25.nipic.com/20121112/9252150_150552938000_2.jpg");
//        imageList.add("http://pic25.nipic.com/20121112/9252150_150552938000_2.jpg");
//        imageList.add("http://pic26.nipic.com/20121221/9252150_142515375000_2.jpg");
//        imageList.add("http://pic26.nipic.com/20121221/9252150_142515375000_2.jpg");
//        imageList.add("http://pic13.nipic.com/20110409/7119492_114440620000_2.jpg");
//        imageList.add("http://pic13.nipic.com/20110409/7119492_114440620000_2.jpg");
//        imageList.add("http://pic68.nipic.com/file/20150601/8164280_104301508000_2.jpg");
//        imageList.add("http://pic68.nipic.com/file/20150601/8164280_104301508000_2.jpg");
//        imageList.add("http://pic68.nipic.com/file/20150601/8164280_104301508000_2.jpg");
//        imageList.add("http://pic68.nipic.com/file/20150601/8164280_104301508000_2.jpg");
//        imageList.add("http://pic68.nipic.com/file/20150601/8164280_104301508000_2.jpg");
        if (imageList.size() > 7) {
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            layoutParams.width = DensityUtils.dp2px(mContext, (128 * 6) + 114);
            recyclerView.setLayoutParams(layoutParams);
        }

        layoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new ProgrammeImageRvAdapter(mContext, R.layout.layout_programme_detail_image_rv_item, imageList);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter1, View view, int position) {
                EventBus.getDefault().post(new ImageClickEvent(imageList.get(position), position));

                adapter.setSelectPos(position);
                adapter.notifyDataSetChanged();
            }
        });
        recyclerView.setAdapter(adapter);

        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                rvFirstVisibleItem = layoutManager.findFirstVisibleItemPosition();
                rvLastVisibleItem = layoutManager.findLastVisibleItemPosition();
            }
        });
    }

    /**
     * 继承GestureListener，重写left和right方法
     */
//    private class MyGestureListener extends GestureListener {
//        public MyGestureListener(Context context) {
//            super(context);
//        }
//
//        @Override
//        public boolean left(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
//            Log.e("test", "向左滑");
//            if (imageList.size() > 0) {
//                mView.getIvMain().setAnimation(AnimationUtils.loadAnimation(mContext, R.anim.push_left_out1));
//
//                if (adapter.getSelectPos() < imageList.size() - 1) {
//                    Glide.with(mContext)
//                            .load(imageList.get(adapter.getSelectPos() + 1))
//                            .into(mView.getIvMain());
//                    adapter.setSelectPos(adapter.getSelectPos() + 1);
//                    mView.getTvSelect().setText("" + (adapter.getSelectPos() + 1));
//                    adapter.notifyDataSetChanged();
//                }
//
//                new Handler().postDelayed(new Runnable() {
//                    @Override
//                    public void run() {
//                        mView.getIvMain().setAnimation(AnimationUtils.loadAnimation(mContext, R.anim.push_left_in1));
//                    }
//                }, 250);
//
//            }
//            return super.left(e1, e2, velocityX, velocityY);
//        }
//
//        @Override
//        public boolean right(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
//            Log.e("test", "向右滑");
//            if (imageList.size() > 0) {
//                mView.getIvMain().setAnimation(AnimationUtils.loadAnimation(mContext, R.anim.push_right_in1));
//
//                if (adapter.getSelectPos() > 0) {
//                    Glide.with(mContext)
//                            .load(imageList.get(adapter.getSelectPos() - 1))
//                            .into(mView.getIvMain());
//                    adapter.setSelectPos(adapter.getSelectPos() - 1);
//                    mView.getTvSelect().setText("" + (adapter.getSelectPos() + 1));
//                    adapter.notifyDataSetChanged();
//                }
//                new Handler().postDelayed(new Runnable() {
//                    @Override
//                    public void run() {
//                        mView.getIvMain().setAnimation(AnimationUtils.loadAnimation(mContext, R.anim.push_right_out1));
//                    }
//                }, 250);
//
//            }
//            return super.right(e1, e2, velocityX, velocityY);
//        }
//    }
    public void resetImageSize(Bitmap bitmap, int height, int width) {
        int targetWidth = width;
        int targetHeight = height;
        //计算缩放比例
        float sy;
        if (height > width) {//竖图
            if (height > 450) {
                targetHeight = 450;
                sy = (float) (targetHeight * 0.1) / (float) (height * 0.1);
                targetWidth = (int) (width * sy);
            }

        } else if (height == width) {

        } else {//横图
            if (width > DensityUtils.dp2px(mContext, 400) && height > DensityUtils.dp2px(mContext, 300)) {
                targetHeight = DensityUtils.dp2px(mContext, 300);
                targetWidth = DensityUtils.dp2px(mContext, 400);
            } else if (width > 600) {
                targetWidth = 600;
                sy = (float) (targetWidth * 0.1) / (float) (width * 0.1);
                targetHeight = (int) (height * sy);
            }
        }
        Log.d("haoge--->", bitmap.getHeight() + "--" + bitmap.getWidth() + "---->" + targetHeight + "---" + targetWidth);

        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        layoutParams.width = targetWidth;
        layoutParams.height = targetHeight;
        if (null != mView) {
            mView.getRImage().setLayoutParams(layoutParams);
        }

        //mView.getIvMain().setImageBitmap(bitmap);
    }
}
